<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/login.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
</head>

<body>
<div class="container-login">
    <div class="container-pic">
        <img src="pic/computer.png" width="350px">
    </div>
    <div class="login-dialog">
        <h3>登陆</h3>
        <div class="row">
            <span>用户名</span>
            <input type="text" name="userName" id="userName" class="form-control">
        </div>
        <div class="row">
            <span>密码</span>
            <input type="password" name="password" id="password" class="form-control">
        </div>
        <div class="row">
            <button type="button" class="btn btn-info btn-lg" onclick="login()">登录</button>
        </div>
    </div>
</div>
<script src="js/jquery.min.js"></script>
<script>
    function login() {
        // 前端参数校验
        var userName = $("#userName").val().trim();
        var password = $("#password").val().trim();
        
        if (!userName) {
            alert("用户名不能为空");
            $("#userName").focus();
            return;
        }
        if (!password) {
            alert("密码不能为空");
            $("#password").focus();
            return;
        }
        $.ajax({
            type: "post",
            url: "/user/login",
            data: {
                userName: $("#userName").val(),
                password: $("#password").val()
            },
            success: function(result){
                // 后端login接口返回boolean，会被ResponseAdvice包装成Result格式
                if(result && result.code == "SUCCESS" && result.data == true){
                    //账号密码正确，存储登录状态到localStorage
                    localStorage.setItem('userLoggedIn', 'true');
                    localStorage.setItem('loginTime', new Date().getTime());
                    location.href = "book_list.html?currentPage=1";
                }else {
                    alert("账号或密码错误");
                }
            },
            error: function(xhr, status, error) {
                alert("登录请求失败，请重试");
            }

        });
        // location.href = "book_list.html";
    }
</script>
</body>

</html>